package cn.com.gridinfo.sooc.core.home.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.Log;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.XnMainFragment;
import cn.com.gridinfo.sooc.core.expand.ExpandFragment;
import cn.com.gridinfo.sooc.core.my.fragment.MyMainFragment;

/**
 * Created by jieli on 15/12/9.
 */
public class HomePagerAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 3;
//    private String tabTitles[] = new String[]{"校内", "公开课", "发现","我的"};
    private String tabTitles[] = new String[]{"首页", "发现","我的"};
//    private Integer imageResId[] = new Integer[]{R.drawable.tab_xn_image,R.drawable.tab_openclass_image, R.drawable.tab_expand_image,R.drawable.tab_my_image};
    private Integer imageResId[] = new Integer[]{R.drawable.tab_xn_image, R.drawable.tab_expand_image, R.drawable.tab_my_image};

    private Context context;
    private final FragmentManager mFragmentManager;

    public View getTabView(int position) {
        View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) v.findViewById(R.id.textView);
        tv.setText(tabTitles[position]);
        ImageView img = (ImageView) v.findViewById(R.id.imageView);
        img.setImageResource(imageResId[position]);

        return v;
    }
    public void setTabView(int position){

    }

    public HomePagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        mFragmentManager = fm;
        this.context = context;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        Log.d("============page position=====["+position+"]==");
        switch (position) {
            case 0://校内
                fragment = XnMainFragment.newInstance();
                break;
//            case 1://公开课
//                fragment = OpenClassFragment.newInstance();
//                break;
            case 1://发现
                fragment = ExpandFragment.newInstance();
                break;
            case 2://个人信息
                fragment = MyMainFragment.newInstance();
                break;
        }
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }

//    @Override
//    public Object instantiateItem(ViewGroup container, int position) {
//        Fragment fragment = getItem(position);
//        if(!fragment.isAdded()){
//            FragmentTransaction ft = mFragmentManager.beginTransaction();
//            ft.add(fragment, fragment.getClass().getName());
//            ft.commit();
//            mFragmentManager.executePendingTransactions();
//        }
//        if(fragment.getView().getParent() == null){
//            container.addView(fragment.getView()); // 为viewpager增加布局
//        }
//
//        return fragment;
//    }
//
//    @Override
//    public void destroyItem(ViewGroup container, int position, Object object) {
//        container.removeView(((Fragment)object).getView());
//    }
}
